package com.rxyb.redis.utils;

import com.rxyb.redis.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis hash类型工具类
 *
 * @author lcf
 * @date 2020/6/5
 */
@Slf4j
@Service
public class RedisHashUtils {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * redis hash存值
     *
     * @param key
     * @param map
     * @return
     */
    public boolean hset(String group, String key, Map<String, Object> map, TimeUnit timeUnit, long times) {
        try {
            redisTemplate.opsForHash().putAll(RedisConstant.REDIS_HASH + "_" + group + "_" + key, map);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(RedisConstant.REDIS_HASH + "_" + group + "_" + key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key      redis key
     * @param mapKey   hash key
     * @param value    hash value
     * @param times
     * @param timeUnit
     * @return
     */
    public boolean hset(String group, String key, String mapKey, Object value, long times, TimeUnit timeUnit) {
        try {
            redisTemplate.opsForHash().put(RedisConstant.REDIS_HASH + "_" + group + "_" + key, mapKey, value);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(RedisConstant.REDIS_HASH + "_" + group + "_" + key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * redis hash存值
     *
     * @param key
     * @param map
     * @return
     */
    public boolean hset(String key, Map<String, Object> map, TimeUnit timeUnit, long times) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key      redis key
     * @param mapKey   hash key
     * @param value    hash value
     * @param times
     * @param timeUnit
     * @return
     */
    public boolean hset(String key, String mapKey, Object value, long times, TimeUnit timeUnit) {
        try {
            redisTemplate.opsForHash().put(key, mapKey, value);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取hash key对应的所有键值
     *
     * @param key
     * @return
     */
    public Optional<Map<Object, Object>> hget(String key) {
        Map map = redisTemplate.opsForHash().entries(key);
        if (null == map) {
            return Optional.empty();
        }
        return Optional.of(map);
    }

    /**
     * 根据redis key和hash key 获取值
     *
     * @param key
     * @param mapKey
     * @return
     */
    public Optional<Object> hget(String key, String mapKey) {
        Object object = redisTemplate.opsForHash().get(key, mapKey);
        if (null == object) {
            return Optional.empty();
        }
        return Optional.of(object);
    }

    /**
     * 根据key获取redis的值
     *
     * @param key
     * @return
     */
    public Optional<Object> get(String group, String key) {
        Object object = redisTemplate.opsForValue().get(RedisConstant.REDIS_HASH + "_" + group + "_" + key);
        if (null == object) {
            return Optional.empty();
        }
        return Optional.of(object);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String group, String key) {
        redisTemplate.delete(RedisConstant.REDIS_HASH + "_" + group + "_" + key);
    }

    /**
     * 根据key删除redis
     *
     * @param group
     */
    public void delgroup(String group) {
        //最后一定要带上 *
        Set<String> keys = redisTemplate.keys(RedisConstant.REDIS_HASH + "_" + group + "_*");
        redisTemplate.delete(keys);
    }

    /**
     * 判断该hash表中是否有该项的值
     *
     * @param key
     * @param mapKey
     * @return
     */
    public boolean isExist(String key, String mapKey) {
        return redisTemplate.opsForHash().hasKey(key, mapKey);
    }
}
